﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Assesment.Web.Support;
using Assesment.Library.DomainObject.Administration.UserManagement;


namespace Assesment.Web.Forms.Administration.Catalog
{

    public partial class LevelJabatanForm :  BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            try
            {

                if (Request.Params["id"] != null)
                {
                    id = Convert.ToInt32(Request.Params["id"]);
                }

               

                if (!Page.IsPostBack)
                {
                    if (id > 0) // Edit an existing item
                    {
                        LevelJabatanRepo LevelJabatanDal = new LevelJabatanRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                        LevelJabatan obj = LevelJabatanDal.GetById(id);
                        if (obj != null)
                        {
                            hfID.Value = id.ToString();
                            txtNama.Text = obj.NamaLevel;
                        }
                    }

                    GetPrivilege();


                }
            }
            catch (Exception ex) {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" +" Error :" +ex.Message + "');", true);
                return;
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }

}
